package com.javalong.affairs.module.renwujinzhang.ui;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.javalong.affairs.R;
import com.javalong.affairs.base.common.UIStatus;
import com.javalong.affairs.base.utils.DateTimePickDialogUtil;
import com.javalong.affairs.base.utils.DateUtils;
import com.javalong.affairs.base.utils.StringUtils;
import com.javalong.affairs.base.utils.TimeUtils;

import java.util.Date;

/**
 * Created by 杨观回 on 2016/2/13 0013.
 */
public class SelectTimeView extends FrameLayout implements UIValue {

    private ImageView iv_left;
    private TextView tv_time;
    private Button bt_setting;
    private DateTimePickDialogUtil dialog;

    public SelectTimeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SelectTimeView(Context context) {
        super(context);
        init();
    }

    private void init() {
        Date nowDate = new Date();
        dialog = new DateTimePickDialogUtil((Activity) getContext(),
                DateUtils.formatDate(nowDate));
        LayoutInflater.from(getContext()).inflate(R.layout.item_select_time, this);
        tv_time = (TextView) findViewById(R.id.tv_time);
        iv_left = (ImageView) findViewById(R.id.iv_left);
        bt_setting = (Button) findViewById(R.id.bt_setting);
        bt_setting.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dateTimePicKDialog(tv_time);
            }
        });
        ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        setLayoutParams(param);
    }

    public void setHintText(String text) {
        if (StringUtils.isEmpty(tv_time.getText().toString())) {
            tv_time.setText(text);
        }
    }

    public void setStatus(UIStatus uiStatus) {
        switch (uiStatus) {
            case WATCH:
                bt_setting.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public Object getValue() {
        return TimeUtils.getTimeStr(dialog.getSeconds());
    }

    public void setValue(long value) {
        tv_time.setText(TimeUtils.getFormatTime2(value));
    }
}
